﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace _090.读取文件夹
{

	using System;
	using System.Collections.Generic;
	using System.Text.RegularExpressions;

	public class Program
	{
		public class Data
		{
			public uint Value1 { get; set; }
			public uint Value2 { get; set; }
		}

		public static void Main(string[] args)
		{
			string input = @"header {
            // Created by VbfSign build: 2017/12/12 on 2023/4/16 10:36
            sw_part_number = ""6608108771"";
            sw_version = ""A"";
            sw_part_type = SBL;
            data_format_identifier = 0x00; // sadsadas
            ecu_address = 0x1015;
            erase = { { /*    start address     */ 0x00004200, /*   Length*/ 0x00003CBF } };
            file_checksum = 0x77007B6A;
            call = 0x953FF000;
            verification_block_start = 0x953FFF00;
            verification_block_length = 0x0000002C;
            verification_block_root_hash = 0xEEFC7C43625959BD26AD78D74A22A3FB2D2DB95B460F00BE2084C9C3D3A9A4F5;
        }";

			string pattern = @"(/\*(.|[\r\n])*?\*/)|(//.*)";
			string result = Regex.Replace(input, pattern, String.Empty, RegexOptions.Multiline);
			result = result.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
            Console.WriteLine(result);

			Dictionary<string, string> keyValuePairs = ExtractValues(result);
			foreach (var pair in keyValuePairs)
			{
				Console.WriteLine($"{pair.Key}: {pair.Value}");
			}
		}

		/**
		 * 	(\w+)：这是一个捕获组，用于匹配一个或多个字母数字字符（包括下划线）。它表示键的部分。

			=：匹配等号字符。

			(\""?[\w\{\}\=\,]+\""?)：这也是一个捕获组，用于匹配值的部分。其中包括以下部分：

			\""?：这部分用于匹配值的起始引号，可以是0个或1个引号。这是为了兼容可能存在引号的情况。

			[\w\{\}\=\,]+：这部分匹配一个或多个字母数字字符、大括号、等号或逗号。这是为了匹配值中可能出现的字符。

			\""?：这部分用于匹配值的结束引号，可以是0个或1个引号。同样是为了兼容可能存在引号的情况。

			(?=;)：这是一个零宽度正向先行断言，用于匹配后面紧跟着分号的位置。它表示这个键值对的结束位置
					 */
		static Dictionary<string, string> ExtractValues(string input)
		{
			Dictionary<string, string> result = new Dictionary<string, string>();


			string pattern = @"(\w+)=(\""?[\w\{\}\=\,]+\""?)(?=;)";

			MatchCollection matches = Regex.Matches(input, pattern);
			foreach (Match match in matches)
			{
				string key = match.Groups[1].Value;
				string value = match.Groups[2].Value.Trim('"');

				Console.WriteLine($"{key}: {value}");
			}

			return result;
		}


		public static void pirntS()
		{
			//string input = "{{0x00004200,0x00003CBF},{0x00004200,0x00003CBF}}";
			string input = "{{0x00004200,0x00003CBF}}";
			var result = ParseInput(input);

			Console.WriteLine("Parsed data:");
			foreach (var item in result)
			{
				Console.WriteLine($"(0x{item.Value1:X8}, 0X{item.Value2:X8})");
			}
		}
		/// <summary>
		/// 匹配 string input = "{{0x00004200,0x00003CBF},{0x00004200,0x00003CBF}}";的形式
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public static List<Data> ParseInput(string input)
		{
			string pattern = @"(\w+),(\w+)";
			List<Data> data = new List<Data>();

			MatchCollection matches = Regex.Matches(input, pattern);
			foreach (Match match in matches)
			{
				string value1Hex = match.Groups[1].Value;
				string value2Hex = match.Groups[2].Value;

				// 将十六进制转换为整数
				uint value1 = Convert.ToUInt32(value1Hex, 16);
				uint value2 = Convert.ToUInt32(value2Hex, 16);

				data.Add(new Data { Value1 = value1, Value2 = value2 });
			}
			return data;
		}
	}
}
